home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / jrh-rkrm-partone / intuition / io_methods / eventloop.e < prev   
Text File  |  1995-03-26  |  5KB  |  131 lines

  1. -> eventloop.e - standard technique to handle IntuiMessages from an IDCMP.
  2.  
  3. MODULE 'exec/ports',
  4.        'intuition/intuition'
  5.  
  6. ENUM ERR_NONE, ERR_WIN
  7.  
  8. RAISE ERR_WIN IF OpenWindowTagList()=NIL
  9.  
  10. PROC main() HANDLE
  11.   DEF signals, done, win=NIL:PTR TO window
  12.   win:=OpenWindowTagList(NIL,
  13.                         [WA_TITLE,       'Press Keys and Mouse in this Window',
  14.                          WA_WIDTH,       500,
  15.                          WA_HEIGHT,      50,
  16.                          WA_ACTIVATE,    TRUE,
  17.                          WA_CLOSEGADGET, TRUE,
  18.                          WA_RMBTRAP,     TRUE,
  19.                          WA_IDCMP, IDCMP_CLOSEWINDOW OR IDCMP_VANILLAKEY OR
  20.                             IDCMP_RAWKEY OR IDCMP_DISKINSERTED OR
  21.                             IDCMP_DISKREMOVED OR IDCMP_MOUSEBUTTONS,
  22.                          NIL])
  23.  
  24.   -> Perform this loop until the message handling routine signals that we
  25.   -> are done.
  26.   ->
  27.   -> When the Wait() returns, check which signal hit and process the correct
  28.   -> port.  There is only one port here, so the test could be eliminated.  If
  29.   -> multiple ports were being watched, the test would become:
  30.   ->
  31.   ->    signals:=Wait(Shl(1, win1.userport.sigbit) OR
  32.   ->                  Shl(1, win2.userport.sigbit) OR
  33.   ->                  Shl(1, win3.userport.sigbit))
  34.   ->    IF signals AND Shl(1, win1.userport.sigbit)
  35.   ->      done:=handleWin1IDCMP(win1, done)
  36.   ->    ELSEIF signals AND Shl(1, win2.userport.sigbit)
  37.   ->      done:=handleWin2IDCMP(win2, done)
  38.   ->    ELSEIF signals AND Shl(1, win3.userport.sigbit)
  39.   ->      done:=handleWin3IDCMP(win3, done)
  40.   ->    ENDIF
  41.   ->
  42.   -> Note that these could all call the same routine with different window
  43.   -> pointers (if the handling was identical).
  44.   ->
  45.   -> handleIDCMP() should remove all of the messages from the port.
  46.   -> E-Note: since this example should be generalisable to more than one
  47.   ->         window, WaitIMessage is not used (for a change!)
  48.   done:=FALSE
  49.   REPEAT
  50.     signals:=Wait(Shl(1, win.userport.sigbit))
  51.     IF signals AND Shl(1, win.userport.sigbit)
  52.       done:=handleIDCMP(win, done)
  53.     ENDIF
  54.   UNTIL done
  55.  
  56. EXCEPT DO
  57.   IF win THEN CloseWindow(win)
  58.   SELECT exception
  59.   CASE ERR_WIN; WriteF('Error: Failed to open window.\n')
  60.   ENDSELECT
  61. ENDPROC
  62.  
  63. -> handleIDCMP() - Handle all of the messages from an IDCMP.
  64. PROC handleIDCMP(win:PTR TO window, done)
  65.   DEF message:PTR TO intuimessage, code, mousex, mousey, class
  66.  
  67.   -> Remove all of the messages from the port by calling GetMsg() until
  68.   -> it returns NULL.
  69.   ->
  70.   -> The code should be able to handle three cases:
  71.   ->
  72.   -> 1.  No messages waiting at the port, and the first call to GetMsg()
  73.   -> returns NULL.  In this case the code should do nothing.
  74.   ->
  75.   -> 2.  A single message waiting.  The code should remove the message,
  76.   -> processes it, and finish.
  77.   ->
  78.   -> 3.  Multiple messages waiting.  The code should process each waiting
  79.   -> message, and finish.
  80.   WHILE message:=GetMsg(win.userport)
  81.     -> It is often convenient to copy the data out of the message.  In many
  82.     -> cases, this lets the application reply to the message quickly.  Copying
  83.     -> the data is not required, if the code does not reply to the message
  84.     -> until the end of the loop, then it may directly reference the message
  85.     -> information anywhere before the reply.
  86.     class:=message.class
  87.     code:=message.code
  88.     mousex:=message.mousex
  89.     mousey:=message.mousey
  90.  
  91.     -> The loop should reply as soon as possible.  Note that the code may not
  92.     -> reference data in the message after replying to the message.  Thus, the
  93.     -> application should not reply to the message until it is done referencing
  94.     -> information in it.
  95.     ->
  96.     -> Be sure to reply to every message received with GetMsg().
  97.     ReplyMsg(message)
  98.  
  99.     -> The class contains the IDCMP type of the message.
  100.     SELECT class
  101.     CASE IDCMP_CLOSEWINDOW
  102.       done:=TRUE
  103.     CASE IDCMP_VANILLAKEY
  104.       WriteF('IDCMP_VANILLAKEY (\c)\n', code)
  105.     CASE IDCMP_RAWKEY
  106.       WriteF('IDCMP_RAWKEY\n')
  107.     CASE IDCMP_DISKINSERTED
  108.       WriteF('IDCMP_DISKINSERTED\n')
  109.     CASE IDCMP_DISKREMOVED
  110.       WriteF('IDCMP_DISKREMOVED\n')
  111.     CASE IDCMP_MOUSEBUTTONS
  112.       -> The code often contains useful data, such as the ASCII value (for
  113.       -> IDCMP_VANILLAKEY), or the type of button event here.
  114.       SELECT code
  115.       CASE SELECTUP
  116.         WriteF('SELECTUP at \d,\d\n', mousex, mousey)
  117.       CASE SELECTDOWN
  118.         WriteF('SELECTDOWN at \d,\d\n', mousex, mousey)
  119.       CASE MENUUP
  120.         WriteF('MENUUP\n')
  121.       CASE MENUDOWN
  122.         WriteF('MENUDOWN\n')
  123.       DEFAULT
  124.         WriteF('UNKNOWN CODE\n')
  125.       ENDSELECT
  126.     DEFAULT
  127.       WriteF('Unknown IDCMP message\n')
  128.     ENDSELECT
  129.   ENDWHILE
  130. ENDPROC done
  131.